package com.wobangkj.common.api.crud;

import com.alibaba.fastjson.JSONObject;
import com.wobangkj.common.model.crud.Liked;
import com.wobangkj.common.service.LikedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/liked")
public class LikedController {
    @Autowired
    private LikedService likedService;

    @PostMapping(value = "/create")
    public Object create(@RequestBody Liked data) {
        // 创建时间
        data.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
        Long a=Long.parseLong(data.getLikedContentId());
        if(data.getLikedClass()==0){
            likedService.updateShareNumById(a);
            String userId = likedService.getSharerId(data.getLikedContentId());
            data.setLikedContentPublisherId(userId);
            return likedService.create(data);
        }
        else if (data.getLikedClass()==1){
            likedService.updateDynamicNumById(a);
            String userId = likedService.getDynamicPublisherId(data.getLikedContentId());
            data.setLikedContentPublisherId(userId);
            return likedService.create(data);
        }
        else {
            likedService.updateCommentNumById(a);
            String userId = likedService.getCommentPublisherId(data.getLikedContentId());
            data.setLikedContentPublisherId(userId);
            return likedService.create(data);
        }
    }

    @PostMapping(value = "/findById")
    public Object findById(String likerId,String likedContentId, String likedClass) {
        return likedService.findById(likerId,likedContentId,likedClass);
    }


    @PostMapping(value = "/delete")
    public Object delete(String likerId,String likedContentId,Long likedClass) {
        Long id = likedService.findLiked (likerId,likedContentId,likedClass.toString());
        if(likedClass==0){
            likedService.deleteShareNumById(id);
            return likedService.delete(id);
        }
        else if (likedClass==1){
            likedService.deleteDynamicNumById(id);
            return likedService.delete(id);
        }
        else {
            likedService.deleteCommentNumById(id);
            return likedService.delete(id);
        }
    }

    @PostMapping(value = "/findMy")
    public Object findMy(String id){
        return likedService.findMy(id);
    }
}
